<g:javascript src="jquery/jquery-jtemplates.js"/>
<script language="javascript">
    $(document).ready(function() {       
       $("#submitCourse").click(function(){ 
            $('#morningCourse').html("")
            $('#afternoonCourse').html("")
            $("#morningCourse").setTemplateElement("templateleCourse");
            var json1 = new Array(parseInt($("#morning").val()));
            for(x = 0 ; x < parseInt($("#morning").val()) ; x++){
                json1[x] = x+1;
            } 
            $("#morningCourse").processTemplate({"employee":json1});
            
            var json2 = new Array(parseInt($("#afternoon").val()));
            for(x = 0 ; x < parseInt($("#afternoon").val()) ; x++){
                json2[x] = x+1;
            } 
            $("#afternoonCourse").setTemplateElement("templateleAfternoonCourse");
            $("#afternoonCourse").processTemplate({"employee":json2});
       })

       $("#restTimeCreate").submit( function () {
            elemlength = $(".muchMorningCourse").length;
            if(!vaildCourseTime('selectMorning',elemlength)){
                return false;
            }
            elemlength1 = $(".muchAfternoonCourse").length;
            if(!vaildCourseTime('selectAfternoon',elemlength1)){                
                return false;
            }
            if(elemlength == 0 && elemlength1 == 0){
                alert("请选择上午下午的课时！")
                return false;
            }
            $("#restTimeList").val("")
            setRestTimeList('selectMorning',elemlength);
            setRestTimeList('selectAfternoon',elemlength1);
            return true;
        });
    });

       function vaildCourseTime(obj,elemlength){
            var time
            if("selectMorning"==obj){
                time = "上午"
            }else{
                time = "下午"
            }
            for(i=0;i<parseInt(elemlength);i++){
                // 确定是那一课
                num = $("#"+obj+"StartHour"+(i+1)).attr("xeduCourse")
                // 某课的起始时间
                oneCourseTime  = parseInt($("#"+obj+"StartHour"+(i+1)).val()+$("#"+obj+"StartMinute"+(i+1)).val())
                // 某课的结束时间
                oneEndCourseTime  = parseInt($("#"+obj+"EndHour"+(i+1)).val()+$("#"+obj+"EndMinute"+(i+1)).val())
                if(oneCourseTime >= oneEndCourseTime){
                    alert(time+"，第"+num+"节课，下课时间与上课时间有冲突！")
                    return false;
                }else{
                    for(m=0;m<elemlength;m++){
                        num1 = $("#"+obj+"StartHour"+(m+1)).attr("xeduCourse")
                        if(num != num1){
                            // 某课的起始时间
                            oneCourseTime1  = parseInt($("#"+obj+"StartHour"+(m+1)).val()+$("#"+obj+"StartMinute"+(m+1)).val())
                            // 某课的结束时间
                            oneEndCourseTime1  = parseInt($("#"+obj+"EndHour"+(m+1)).val()+$("#"+obj+"EndMinute"+(m+1)).val())

                            if(oneCourseTime1 >=  oneCourseTime && oneCourseTime1 <= oneEndCourseTime){
                                alert(time+"，第"+num+"节课和"+"第"+num1+"节课时间上冲突了！")
                                return false;
                            }
                        }                    
                    }
                }
            }
            return true
        }

        function setRestTimeList(obj,elemlength){
            
            for(i=0;i<parseInt(elemlength);i++){
                startHour = $("#"+obj+"StartHour"+(i+1)).val()
                startMinute = $("#"+obj+"StartMinute"+(i+1)).val()
                
                endHour = $("#"+obj+"EndHour"+(i+1)).val()
                endMinute = $("#"+obj+"EndMinute"+(i+1)).val()
                $("#restTimeList").val($("#restTimeList").val()+startHour+","+startMinute+","+endHour+","+endMinute+"|")
            }
        }
</script>